package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.QueryCycleTypeInstanceRelationshipDetailReq;
import com.sg.dto.base.app.req.QueryCycleTypeInstanceRelationshipListReq;
import com.sg.entity.OmsCycleTypeInstanceRelationship;
import com.sg.service.base.app.MOmsCycleTypeInstanceRelationshipService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsCycleTypeInstanceRelationshipMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsCycleTypeInstanceRelationshipServiceImpl
        extends ServiceImpl<OmsCycleTypeInstanceRelationshipMapper, OmsCycleTypeInstanceRelationship>
        implements MOmsCycleTypeInstanceRelationshipService {

    @Autowired
    OmsCycleTypeInstanceRelationshipMapper omsCycleTypeInstanceRelationshipMapper;

    /**
     * 3-4-02查工作周期类型关系列表[1873]
     * gen by moon at 8/17/2022, 8:02:15 PM
     */
    @Trace(operationName = "3-4-02查工作周期类型关系列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsCycleTypeInstanceRelationship> queryCycleTypeInstanceRelationshipList(QueryCycleTypeInstanceRelationshipListReq reqDto) {

        try {
            QueryWrapper<OmsCycleTypeInstanceRelationship> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsCycleTypeInstanceRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsCycleTypeInstanceRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsCycleTypeInstanceRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsCycleTypeInstanceRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-4-03新增工作周期类型关系[1871]
     * gen by moon at 8/17/2022, 8:02:14 PM
     */
    @Trace(operationName = "3-4-03新增工作周期类型关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addCycleTypeInstanceRelationship(OmsCycleTypeInstanceRelationship omsCycleTypeInstanceRelationship) {
        omsCycleTypeInstanceRelationship.setCycleRelationshipId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsCycleTypeInstanceRelationship.setIsArchive("FALSE");
        omsCycleTypeInstanceRelationship.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsCycleTypeInstanceRelationship.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsCycleTypeInstanceRelationship != null && omsCycleTypeInstanceRelationship.getSubjectLifeCycle() != null && omsCycleTypeInstanceRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsCycleTypeInstanceRelationship.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsCycleTypeInstanceRelationship.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsCycleTypeInstanceRelationship.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsCycleTypeInstanceRelationship != null && omsCycleTypeInstanceRelationship.getSubjectLifeCycle() != null && omsCycleTypeInstanceRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsCycleTypeInstanceRelationship.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsCycleTypeInstanceRelationship.setIsValid("FALSE");
        omsCycleTypeInstanceRelationship.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsCycleTypeInstanceRelationship.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsCycleTypeInstanceRelationship)) {
            return omsCycleTypeInstanceRelationship.getCycleRelationshipId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-4-02修改工作周期类型关系[1872]
     * gen by moon at 8/17/2022, 8:02:14 PM
     */
    @Trace(operationName = "3-4-02修改工作周期类型关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCycleTypeInstanceRelationship(OmsCycleTypeInstanceRelationship omsCycleTypeInstanceRelationship) {
        omsCycleTypeInstanceRelationship.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsCycleTypeInstanceRelationship != null && omsCycleTypeInstanceRelationship.getSubjectLifeCycle() != null && omsCycleTypeInstanceRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsCycleTypeInstanceRelationship.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsCycleTypeInstanceRelationship.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsCycleTypeInstanceRelationship != null && omsCycleTypeInstanceRelationship.getSubjectLifeCycle() != null && omsCycleTypeInstanceRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsCycleTypeInstanceRelationship.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        return super.updateById(omsCycleTypeInstanceRelationship);
    }

    /**
     * 3-4-02查工作周期类型关系详情[1895]
     * gen by moon at 4/7/2023, 6:58:32 PM
     */
    @Trace(operationName = "3-4-02查工作周期类型关系详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsCycleTypeInstanceRelationship queryCycleTypeInstanceRelationshipDetail(QueryCycleTypeInstanceRelationshipDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsCycleTypeInstanceRelationship> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsCycleTypeInstanceRelationship> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsCycleTypeInstanceRelationship retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-4-02删除工作周期类型关系[1874]
     * gen by moon at 8/17/2022, 8:02:15 PM
     */
    @Trace(operationName = "3-4-02删除工作周期类型关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCycleTypeInstanceRelationship(String cycleRelationshipId) {

        return super.removeById(cycleRelationshipId);
    }
}
